<template>
  <div>
    <el-row v-for="(item,index) in attachmentdata" :key="index">
      <el-col :span="16">
        <span>{{item.filename}}</span>
      </el-col>
      <el-col :span="4">
        <el-button @click="getUrl(item.file_md5)" size="small">下载</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import { postDataToServer, getDataFromServer } from '../../assets/js/post.js'

  export default {
    name: 'attchmentfile',
    props: ['attachmentdata', 'authority'],
    data () {
      return {
        fileauth: false,
        fileexpires: '',
        filesign: '',
        downurl: '',
        fileurl: ''
      }
    },
    methods: {
      getUrl (val) {
        let _this = this
        let params = new Object()
        params.file_md5 = val
        params.id = this.authority.id
        postDataToServer(this.authority.key, params)
          .then((data) => {
            if (data.status == 0) {
              _this.getDownParams(data.download_url)
              if (_this.fileauth == 'true') {
                let spara = new Object()
                spara.file_auth = _this.fileauth
                spara.file_expires = _this.fileexpires
                spara.file_sign = _this.filesign
                this.fileurl = getDataFromServer('GET_AUTH_FILE', _this.downurl, spara)
                window.open(this.fileurl, '_blank')
              }
              else {
                window.open(data.download_url, '_blank')
              }

            }
            else {
              this.$message({
                message: '下载链接失效',
                type: 'error'
              })
            }
          })
      },
      getQueryString (down_url) {
        let urlinfo = down_url
        let len = urlinfo.length
        let offset = urlinfo.indexOf('?')
        if (offset == -1) {
          return undefined
        }
        let newsidinfo = urlinfo.substr(offset + 1, len)
        var newinfo = newsidinfo.split('&')
        var MultiID = {}
        for (let i = 0; i < newinfo.length; i++) {
          let newsids = newinfo[i].split('=')
          MultiID[newsids[0]] = newsids[1]
        }
        return MultiID
      },

      getDownParams (down_url) {
        var m = this.getQueryString(down_url)
        this.fileauth = m.file_auth
        this.fileexpires = m.file_expires
        this.filesign = m.file_sign
        m = down_url.split('?')
        this.downurl = m[0]
      },

    }
  }
</script>
